package com.corn3lius.android.hexagonmadness;

import com.corn3lius.android.hexagonmadness.HexView;
import com.corn3lius.android.hexagonmadness.HexView.GameThread;
import com.corn3lius.android.hexagonmadness.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageButton;
import android.widget.TextView;

public class HexagonMadness extends Activity {

	/** A handle to the thread that's actually running the animation. */
	private GameThread mHexThread;

	/** A handle to the View in which the game is running. */
	private HexView mHexView;
	
	/**
	 * Invoked during init to give the Activity a chance to set up its Menu.
	 * 
	 * @param menu
	 *            the Menu to which entries may be added
	 * @return true
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//super.onCreateOptionsMenu(menu);
		Log.i("hexMadness","Menu button Pressed");
	    
		mHexThread.pause();
	    		
		return true;
	}
	@Override 
	public void onBackPressed(){				
		Log.i("hexMadness","Back button Pressed");
		mHexThread.pause();
		/// this partial class will set the hexViews exiting flag to alert this dialog that the app is closing. 
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to exit?")
				.setCancelable(false)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int id) {
								mHexView.exiting = true;								
							}
						})
				.setNegativeButton("No", null );
		
		AlertDialog alert = builder.create();
		alert.show();
		mHexThread.unpause();
		
		if( mHexView.exiting ) 
		{ // user selected to exit the game.			
			super.onBackPressed();		
		}
	}

	/**
	 * Invoked when the Activity is created.
	 * 
	 * @param savedInstanceState
	 *            a Bundle containing state saved from a previous execution, or
	 *            null if this is a new execution
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			
			//mContext = getApplicationContext(); 

			Log.e(HexView.logId,"About to inflate the main xml " );
			
			// tell system to use the layout defined in our XML file
			setContentView(R.layout.main);

			// get handles to the HexView from XML
			mHexView = (HexView) findViewById(R.id.hexagon_game);			
			// give the View a handle to the TextView used for messages
			mHexView.setTextView((TextView) findViewById(R.id.text));
			// and the buttons for the menu
			mHexView.setMenuButton((ImageButton) findViewById(R.id.imageButton1));
			mHexView.setUndoButton((ImageButton) findViewById(R.id.imageButton2));
			// Get The THREAD... 
			mHexThread = mHexView.getThread();
//						 
//			if (savedInstanceState == null) {
//				// we were just launched: set up a new game
//				Log.w(HexView.logId, "SIS is null");
//				mHexThread.setState(HexView.GameThread.STATE_IDLE);
//			} else {
//				// we are being restored: resume a previous game
//				Log.w(HexView.logId, "SIS -- EXISTS");
//				mHexThread.restoreState(savedInstanceState);
//			}
		} catch (Exception e) {

			// this is the line of code that sends a real error message to the
			// log
			Log.e("hexMadness", "ERROR IN CODE: " + e.toString());

			// this is the line that prints out the location in
			// the code where the error occurred.
			e.printStackTrace();
		}
	}

	/**
	 * Invoked when the Activity loses user focus.
	 */
	@Override
	protected void onPause() {
		super.onPause();
		if (mHexThread == null ){
			mHexThread = mHexView.getThread();
		}		
		mHexThread.pause(); // pause game when Activity pauses		
	}

	/**
	 * Notification that something is about to happen, to give the Activity a
	 * chance to save state.
	 * 
	 * @param state
	 *            a Bundle into which this Activity should save its state
	 */
	@Override
	protected void onSaveInstanceState(Bundle state) {
		// just have the View's thread save its state into our Bundle
		super.onSaveInstanceState(state);
		Log.w(HexView.logId, "SIS called");
		//mHexThread.saveState(state);
	}
	// Probably wont fix. 
	// thats too easy. :(
	@Override
	protected void onRestoreInstanceState(Bundle state) {
		super.onRestoreInstanceState(state);
		Log.w(HexView.logId, "RIS Called");
		//mHexThread.restoreState(state);
		
	}	
	
}